﻿
using App.Code;
using App.Entity;
using App.IServices;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace AppAit.Areas.SystemManage.Controllers
{
    public class RoleAuthorizeController : ControllerBase
    {
        ISys_RoleAuthorizeServices _roleAuthorize;
        ISys_ModuleServices _module;
        ISys_ModuleButtonServices _moduleButton;
        public RoleAuthorizeController(ISys_RoleAuthorizeServices roleAuthorize, ISys_ModuleServices module, ISys_ModuleButtonServices moduleButton)
        {
            _roleAuthorize = roleAuthorize;
            _module = module;
            _moduleButton = moduleButton;
        }
        
        /// <summary>
        /// 获取角色权限
        /// </summary>
        /// <param name="roleId">角色id</param>
        /// <returns></returns>
        public ActionResult GetPermissionTree(string roleId)
        {
            var moduledata = _module.GetList();
            var buttondata = _moduleButton.GetList();
            var authorizedata = new List<Sys_RoleAuthorize>();
            if (!string.IsNullOrEmpty(roleId))
            {
                authorizedata = _roleAuthorize.GetList(roleId);
            }
            var treeList = new List<TreeViewModel>();
            foreach (Sys_Module item in moduledata)
            {
                TreeViewModel tree = new TreeViewModel();
                bool hasChildren = moduledata.Count(t => t.ParentId == item.Id) == 0 ? false : true;
                tree.id = item.Id;
                tree.text = item.FullName;
                tree.value = item.EnCode;
                tree.parentId = item.ParentId;
                tree.isexpand = true;
                tree.complete = true;
                tree.showcheck = true;
                tree.checkstate = authorizedata.Count(t => t.ItemId == item.Id);
                tree.hasChildren = true;
                tree.img = item.Icon == "" ? "" : item.Icon;
                treeList.Add(tree);
            }
            foreach (Sys_ModuleButton item in buttondata)
            {
                TreeViewModel tree = new TreeViewModel();
                bool hasChildren = buttondata.Count(t => t.ParentId == item.Id) == 0 ? false : true;
                tree.id = item.Id;
                tree.text = item.FullName;
                tree.value = item.EnCode;
                tree.parentId = item.ParentId == "0" ? item.ModuleId : item.ParentId;
                tree.isexpand = true;
                tree.complete = true;
                tree.showcheck = true;
                tree.checkstate = authorizedata.Count(t => t.ItemId == item.Id);
                tree.hasChildren = hasChildren;
                tree.img = item.Icon == "" ? "" : item.Icon;
                treeList.Add(tree);
            }
            return Content(treeList.TreeViewJson());
        }
    }
}